package com.libo.homework2


import scala.util.Random


object GameStart extends App{

  println("****************************************")
  println("**************猜拳，开始******************")
  println("****************************************")

  println("请选择对战角色：1.刘备 2.关羽 3.张飞")

  var flag = true
  var choseNum: Int = _
  var battleNum: Int = _
  var otherUser: User = _
  var youUser: User = new User("游客")
  while (flag) {
    choseNum = scala.io.StdIn.readInt()

    otherUser = choseNum match {
      case 1 => new User("刘备")
      case 2 => new User("关羽")
      case 3 => new User("张飞")
      case _ => null
    }
    if (otherUser == null) {
      println("只能输入1、2、3，请再次输入。。。")
    } else {
      println(s"你选择了与${otherUser.name}对战")
      flag = false
    }
  }


  var typeFlag = true
  var youChose: Int = _
  var otherType: Int = _
  while (typeFlag) {

    println("请出拳：1.剪刀 2.石头 3.布")

    youChose = scala.io.StdIn.readInt()
    println(s"你出：${youUser.showFirst(youChose)}")

    otherType = Random.nextInt(3)
    println(s"刘备出拳：${otherUser.showFirst(otherType)}")

    judge(youChose, otherType)

    println("是否开启下一轮：")
    if (scala.io.StdIn.readChar() == 'n') typeFlag = false else typeFlag = true
    battleNum += 1
    if (!typeFlag) {
      println("退出游戏！！！")
      println("----------------------------------------------")
      println(s"${otherUser.name} vs 游客")
      println(s"对战次数：$battleNum 次")
      println(s"姓名\t等分\t胜局\t和局\t负局")
      println(s"游客\t${youUser.score(0)}\t${youUser.score(1)}\t${youUser.score(2)}\t${youUser.score(3)}\t")
      println(s"${otherUser.name}\t${otherUser.score(0)}\t${otherUser.score(1)}\t${otherUser.score(2)}\t${otherUser.score(3)}\t")

    }
  }

  /**
    *
    * @param youChose
    * @param otherChose
    * @return 返回 1、0、-1， 返回1表示你赢，返回0表示对方赢，返回-1表示平局
    */
  def judge(youChose: Int, otherChose: Int): Unit = {

    if (youChose == otherChose) {
      println("结果：和局，下次努力")
      youUser.score(0) += 1
      youUser.score(2) += 1
      otherUser.score(0) += 1
      otherUser.score(2)  += 1
    } else if (youChose > otherChose) {
      if (youChose - otherChose == 1) {
        println("结果：恭喜，你赢啦")
        youUser.score(0) += 2
        youUser.score(1) += 1
        otherUser.score(3) += 1
      } else {
        println("结果：你输了，下次努力")
        otherUser.score(0) += 2
        otherUser.score(1) += 1
        youUser.score(3) += 1
      }
    } else {
      if (otherChose - youChose == 1) {
        println("结果：你输了，下次努力")
        otherUser.score(0) += 2
        otherUser.score(1) += 1
        youUser.score(3) += 1
      } else {
        println("结果：恭喜，你赢啦")
        youUser.score(0) += 2
        youUser.score(1) += 1
        otherUser.score(3) += 1
      }
    }
  }


}
